
package main

import (
	"fmt"
)

func main () {
	defer createNoBufferedChannels()

	defer createBufferedChannels()
}

func createNoBufferedChannels () {
	c := make(chan int, 0)
	go func () {
		fmt.Println("start write")
		c <- 1
		fmt.Println("write done")
	}()
	
	<- c // 没有数据会阻塞，直到能取出数据
	fmt.Println("done")
}

func createBufferedChannels () {
	c := make(chan int, 3)

	for i := 0; i < 3; i ++ {
		t := i
		go func () {
			fmt.Printf("start write %d\n", t)
			c <- t
			fmt.Printf("done write %d\n", t)
		}()
	}

	for j :=0; j < 3; j ++ {
		fmt.Println(<-c)
	}
	fmt.Println("done")

}